
#include "config.h"

#if(UART_DEBUG==1)

#include <iom128.h>
#include <stdio.h>
#include "uart.h"
#include "type.h"

#define UART_BAUDRATE 115200

volatile char uart_need_send = 0;
static char uart_send_buf[UART_SENDBUF_LEN]= {0};


void uart_init(void)
{

	UCSR0B = 0x00; //disable while setting baud rate
	UCSR0A = 0x00;

	UCSR0C =(1<<UCSZ01)|(1<<UCSZ00);//8bit+1bit stop

	UBRR0L= (CPU_F/UART_BAUDRATE/16-1)%256;
	UBRR0H= (CPU_F/UART_BAUDRATE/16-1)/256;

	UCSR0B =((1<<TXEN0)|(1<<RXEN0)|(1<<RXCIE0));//RXCEN TXCEN RXCIE

}

void uart_sendbyte(char byte)
{
	while (!(UCSR0A & (1 << UDRE0)));
	UDR0 = byte;
}

void uart_sendstring(char *str)
{
	while (*str)
	{
		uart_sendbyte(*(str++));
	}
}


void uart_printf(char *fmt,...)
{
	va_list ap;
	va_start(ap, fmt);
	vsprintf(uart_send_buf, fmt, ap);
	uart_sendstring(uart_send_buf);
	va_end(ap);
}




#endif
